package com.inspur.his.controller;
import com.inspur.his.common.EncrypDES;
import com.inspur.his.pojo.User;
import com.inspur.his.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/*可以理解为这就是servlet*/
/*登录验证servlet*/
@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping("loginController")
    public ModelAndView login(String username, String password, HttpSession session) throws Exception {
        ModelAndView mv = new ModelAndView();
        HashMap map = new HashMap();
        map.put("username",username);
        map.put("password",password);
        /*String key = "2020@#$2020";
        EncrypDES des = new EncrypDES(key);
        String en_pass = des.encrypt(password);//加密后的密码
        map.put("password",en_pass);*/
        User user = userService.getUserByNameAndPass(map);
        if(null != user){
            session.setAttribute("suser",user);
            mv.addObject("message","登录成功");
            mv.setViewName("index.jsp");
        }else{
            mv.addObject("error","用户名或密码错误，请重新登录！");
            mv.setViewName("login.jsp");
        }
        return mv;
    }
    @RequestMapping("loginoutController")
    public String logout( HttpSession session){
        session.setAttribute("suser",null);
        return  "login.jsp";
    }
}
